<extend name="Public/frame" />
<block name="page-sub-nav">
    <a onclick="admin_db_backup();" class="btn btn-default" href="#" role="button">{$Think.lang.backup_selected}</a>
</block>
<block name="content-box">

    <table class="table table-striped">
        <thead>
            <tr>
                <th><input onchange="admin_table_select(this);" type="checkbox" value="" class="checkbox tablesall" /></th>
                <th>{$Think.lang.table}</th>
                <th>{$Think.lang.record_count}</th>
                <th>{$Think.lang.operation}</th>
            </tr>
        </thead>
        <tbody>
            <foreach name="table_info" item="v">
                <tr>
                    <td><input type="checkbox" value="{$v.name}" class="checkbox tables" /></td>
                    <td>{$v.name}</td>
                    <td>{$v.count}</td>
                    <td>
                        <a href="#" onclick="admin_db_repair('{$v.name}');" title="{$Think.lang.repair}"><span class="glyphicon glyphicon-wrench"></span></a>
                        &nbsp;&nbsp;&nbsp;&nbsp;
                        <a href="{:U('Database/backup?action=exporttable&table='.$v['name'])}" target="_blank" title="{$Think.lang.export}"><span class="glyphicon glyphicon-save"></span></a>
                    </td>
                </tr>
            </foreach>
        </tbody>
    </table>

    <script type="text/javascript">
    $(function(){
        $('input.tables').on('change',function(){
            if(!$(this).is(':checked')){
                $('input.tablesall').prop('checked',false);
            }
        });
    });
    function admin_db_backup(){
        var tables = [];
        var waiting = null;
        var table_backup_dir = null;
        $('input.tables:checked').each(function(i,o){
            tables.push($(o).val());
        });
        var backup_func = function(){
            if(table_backup_dir){
                var table = tables.pop();
                if(table){
                    waiting.content('{$Think.lang.backuping} '+table);
                    $.post( "{:U('Database/backup?action=backuptable')}",
                            {table:table,dir:table_backup_dir},
                            function(data){
                                if(data.status && data.info && data.info=='OK' && data.status==1){
                                    setTimeout(backup_func,1000);
                                }else{
                                    $.defaultFormCallback(data);
                                }
                            }
                    );
                }else{
                    waiting.close();
                    $.dialog.alert('{$Think.lang.backup_success} data/dbbackup/'+table_backup_dir);
                }
            }else{
                waiting.content('{$Think.lang.backup_initing}');
                $.post("{:U('Database/backup?action=backup_init')}",{},function(data){
                    if(data.ok && data.dir){
                        table_backup_dir = data.dir;
                        setTimeout(backup_func,1000);
                    }else{
                        $.defaultFormCallback(data);
                    }
                });
            }
        };
        if(tables.length){
            waiting=$.dialog.tips(lhgdialog_lang.LOADING,10000000,'loading.gif');
            backup_func();
        }else{
            $.dialog.alert('{$Think.lang.no_selected}');
        }
        return false;
    }
    function admin_table_select(obj){
        $('input.tables').prop('checked', $(obj).is(':checked') );
    }
    function admin_db_repair(table) {
        $.post("{:U('Database/backup?action=repairtable')}",{table:table},function(data){
            $.defaultFormCallback(data);
        });
        return false;
    }
    </script>

</block>